package cn.com.headfree.point.service.impl;

import cn.com.headfree.entity.TPersonYearPoint;
import cn.com.headfree.entity.TPersonYearPointDetail;
import cn.com.headfree.entity.TRecordOptLog;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.point.dao.PersonYearPointDao;
import cn.com.headfree.point.service.PersonYearPointDetailService;
import cn.com.headfree.point.service.PersonYearPointService;
import cn.com.headfree.recordlog.service.IRecordLogService;
import cn.com.headfree.salary.dao.SalaryDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.taglibs.standard.lang.jstl.NullLiteral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("personYearPointService")
@Transactional
public class PersonYearPointServiceImpl implements PersonYearPointService {

	private static final Logger log = LogManager.getLogger(PersonYearPointServiceImpl.class);

	@Autowired
	private PersonYearPointDao personYearPointDao;

	@Autowired
	private PersonYearPointDetailService personYearPointDetailService;

	@Autowired
	private SalaryDao salaryDao;

	@Autowired
	private IRecordLogService recordLogService;

	@Override
	public void auditAgreeInfo(String salaryId, User user) {
		//先修改薪档申报表状态，由待审核变更为同意
		salaryDao.updateSalaryState(salaryId,"1",null);
		//再将积分汇总到汇总表
		List<TPersonYearPointDetail> details = personYearPointDetailService.getTPersonYearPointDetailInfo(salaryId);
		//积分汇总表
		if(details != null && details.size() > 0){
			TPersonYearPoint personYearPoint = new TPersonYearPoint();
			personYearPoint.setSalaryId(salaryId);
			personYearPoint.setOrgId(user.getBaseOrgId());//单位ID
			personYearPoint.setCreaterId(user.getFax());
			personYearPoint.setCreaterName(user.getName());
			personYearPoint.setCreateTime(new Date());
			personYearPoint.setUpdaterId(user.getFax());
			personYearPoint.setUpdaterName(user.getName());
			personYearPoint.setUpdateTime(new Date());
			personYearPoint.setDelSign(0);
			personYearPoint.setErpNo(details.get(0).getErpNo());
			personYearPoint.setUserName(details.get(0).getUserName());
			personYearPoint.setDeptId(details.get(0).getDeptId());
			personYearPoint.setDeptName(details.get(0).getDeptName());
			personYearPoint.setYear(details.get(0).getYear());//年度
			personYearPoint.setYearPoint(details.get(0).getYearPoint());//本年度积分
			personYearPoint.setTrimYearPoint(details.get(0).getYearPoint());//调整后年度积分
			personYearPoint.setLastYearPoint(details.get(0).getLastYearPoint());//上年度剩余积分
			personYearPoint.setRemainPoint(details.get(0).getTrimTotalPoint());//剩余积分
			if(details.get(0).getTotalPoint()!=null&&details.get(0).getTrimTotalPoint()!=null){
//				personYearPoint.setUsePoint(details.get(0).getTotalPoint()-details.get(0).getTrimTotalPoint());
//				personYearPoint.setUseReason("薪档调整");
				Double usePoint = details.get(0).getTotalPoint()-details.get(0).getTrimTotalPoint();
				personYearPoint.setUsePoint(usePoint);
				String useReason = "薪档调整";
				if(usePoint<4){
					useReason = "积分未达到4分";
				}else{
					//绩效未获A
					List<TPersonYearPoint> yearEffectList = personYearPointDao.getYearEffectByErpNo(details.get(0).getErpNo(),details.get(0).getYear());
					if(yearEffectList.size()==0){
						useReason = "绩效未获A或未获得专家人才";
						//专家人才
						List<TPersonYearPoint> expertTitleList = personYearPointDao.getExpertTitleByErpNo(details.get(0).getErpNo(),details.get(0).getYear());
						if(expertTitleList.size()>0){
							useReason = "薪档调整";
						}
					}
				}
				personYearPoint.setUseReason(useReason);
			}
			personYearPoint.setSalaryGrade(details.get(0).getTrimSalaryGrade());//执行岗级
			personYearPointDao.saveInfo(personYearPoint);
			//日志
			TRecordOptLog log = new TRecordOptLog();
			log.setOptType("6");
			log.setOptContent(user.getName()+"核实并同意了该薪档申请");
			log.setCurrentRecordId(salaryId);
			log.setRecordId(salaryId);
			recordLogService.save(user,log);
		}
	}

}
